/**
 * 注册所有需要整合的部署微应用
 */

import {
  registerMicroApps,
  setDefaultMountApp,
  runAfterFirstMounted,
  start
} from 'qiankun';

function render({ appContent, loading }) {
  document.querySelector('#app-loading').style.display = loading
    ? 'block'
    : 'none';
  document.querySelector('#app-container').innerHTML = appContent;
}

function genActiveRule(routerPrefix) {
  return location => location.pathname.startsWith(routerPrefix);
}

/** 跨app全局数据 */
window.microApp = {
  name: 'test'
};

/** 注册所有微应用 */
registerMicroApps(
  [
    {
      // 范例：各类app的整合首页服务（vue）
      name: 'homepage',
      entry: 'http://192.168.1.13:8081',
      render,
      activeRule: () => {
        return (
          location.pathname === '/' || location.pathname.startsWith('/homepage')
        );
      }
    },
    {
      // 范例：活动应用（react）
      name: 'active',
      entry: 'http://192.168.1.13:3000',
      render,
      activeRule: genActiveRule('/active')
    }
  ],
  {
    beforeLoad: [
      app => {
        console.log('应用加载前', app);
      }
    ],
    beforeMount: [
      app => {
        console.log('应用挂载前', app);
      }
    ],
    afterUnmount: [
      app => {
        console.log('应用卸载后', app);
      }
    ]
  }
);

setDefaultMountApp('/');
runAfterFirstMounted(() => console.info('首个应用挂载完毕'));

start();
